//
//  JLTabBarController.m
//  JL网易彩票progress
//
//  Created by sunjinliang on 15/11/6.
//  Copyright © 2015年 sunjinliang. All rights reserved.
//

#import "JLTabBarController.h"
#import "JLArenaViewController.h"
#import "JLDiscoverTableViewController.h"
#import "JLHistoryTableViewController.h"
#import "JLHallTableViewController.h"
#import "JLMineViewController.h"
#import "JLTabBar.h"
#import "JLNavigationController.h"
@interface JLTabBarController ()<UITabBarDelegate>
@property(nonatomic,weak) JLTabBar * customTabBar;
@end

@implementation JLTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTabBar];
    [self setupAllChildController];
    }

//添加tabBar
-(void)setupTabBar{
    
    JLTabBar * customTabBar=[[JLTabBar alloc]init];
    self.customTabBar=customTabBar;
    customTabBar.frame=self.tabBar.bounds;
    [self.tabBar addSubview:customTabBar];
    customTabBar.delegate=(id)self;
}

//删除系统的TabBar的item
-(void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    for (UIView * view in self.tabBar.subviews) {
        if (![view isKindOfClass:[JLTabBar class]]) {
            [view removeFromSuperview];
        }
    }
}

-(void)setupAllChildController{
     [self addAllChildController:@"JLHallTableViewController" WithTitle:@"购彩大厅"  image:@"TabBar_LotteryHall" selectedImage:@"TabBar_LotteryHall_selected"];
     [self addAllChildController:@"JLArenaViewController" WithTitle:@"竞技场" image:@"TabBar_Arena" selectedImage:@"TabBar_Arena_selected"];
     [self addAllChildController:@"JLDiscoverTableViewController" WithTitle:@"发现"  image:@"TabBar_Discovery" selectedImage:@"TabBar_Discovery_selected"];
     [self addAllChildController:@"JLHistoryTableViewController" WithTitle:@"开奖信息" image:@"TabBar_History" selectedImage:@"TabBar_History_selected"];
     [self addAllChildController:@"JLMineViewController" WithTitle:@"我的彩票" image:@"TabBar_MyLottery" selectedImage:@"TabBar_MyLottery_selected"];
    //self.viewControllers=@[arenaVc,discoverVc,hallVc,mineVc,historyVc];

}
-(void)addAllChildController:(NSString *)name WithTitle:(NSString *)title image :(NSString *)image selectedImage :(NSString*)selectedImage{
    UIStoryboard * sb=[UIStoryboard storyboardWithName:name bundle:nil];
    UIViewController * vc=sb.instantiateInitialViewController;
    vc.title=title;
    vc.tabBarItem.image=[UIImage imageNamed:image];
    vc.tabBarItem.selectedImage=[UIImage imageNamed:selectedImage];
    [self.customTabBar addTabBerButton:vc.tabBarItem];
    JLNavigationController * nav=[[JLNavigationController alloc]initWithRootViewController:vc];
    //判断是不是竞技场控制器
    if ([vc isKindOfClass:[JLArenaViewController class]]) {
        nav=(JLNavigationController*)[[UINavigationController alloc]initWithRootViewController:vc];
    }
    [self addChildViewController:nav];
}
//tabBar的代理方法
-(void)TabBar:(JLTabBar *)tabBar Didselectedfrome:(NSInteger)frome to:(NSInteger)to{
    
    self.selectedIndex=to;
//    UIViewController * vc=self.viewControllers[to];
//    self.selectedViewController=vc;
    
}
@end
